import 'dart:convert';

class SocketMessageInfo {
  String messageType;
  String data;
  SocketMessageInfo({
    required this.messageType,
    required this.data,
  });

  SocketMessageInfo copyWith({
    String? messageType,
    String? data,
  }) {
    return SocketMessageInfo(
      messageType: messageType ?? this.messageType,
      data: data ?? this.data,
    );
  }

  Map<String, dynamic> toMap() {
    final result = <String, dynamic>{};

    result.addAll({'messageType': messageType});
    result.addAll({'data': data});

    return result;
  }

  factory SocketMessageInfo.fromMap(Map<String, dynamic> map) {
    String data='';
    if(map['data'] is Map){
      data=json.encode(map['data']);
    }else{
      data=map['data']??'';
    }
    return SocketMessageInfo(
      messageType: map['messageType'] ?? '',
      data: data,
    );
  }

  String toJson() => json.encode(toMap());

  factory SocketMessageInfo.fromJson(String source) =>
      SocketMessageInfo.fromMap(json.decode(source));

  @override
  String toString() =>
      'SocketMessageInfo(messageType: $messageType, data: $data)';

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is SocketMessageInfo &&
        other.messageType == messageType &&
        other.data == data;
  }

  @override
  int get hashCode => messageType.hashCode ^ data.hashCode;
}
